<?php
//https://packagist.org/packages/sabre/event
//https://sabre.io/event/coroutines/
require '../vendor/autoload.php';

use Sabre\Event\Loop;
use Sabre\Event\Emitter;
use Sabre\Event\Promise;
use Sabre\Event;

//$intervalId = Loop\setInterval(function () {
//
//    echo "Hello\n";
//
//}, 1);
//
//Loop\setTimeout(function () {
//    echo "World\n";
//}, 5);

//Loop\run();

function someAsyncOperation()
{
    $promise = new Sabre\Event\Promise(function ($resolve, $reject) {
        Loop\setTimeout(function() use($resolve) {
            $resolve(rand(100, 999));
        }, 1);
    });
    return $promise;
}

function anotherAsyncOperation()
{
    $promise = new Sabre\Event\Promise(function ($resolve, $reject) {
        Loop\setTimeout(function() use($resolve) {
            $resolve(rand(100, 999));
        }, 1);
    });
    return $promise;
}

function lastAsyncOperation()
{
    $promise = new Sabre\Event\Promise(function ($resolve, $reject) {
        Loop\setTimeout(function() use($resolve) {
            $resolve(rand(100, 999));
        }, 1);
    });
    return $promise;
}


function start()
{

    $start = microtime(true);
// 定义一个并发执行的任务
    $concurrentTasks = [
        someAsyncOperation(),
        anotherAsyncOperation(),
        lastAsyncOperation(),
    ];


// 使用Promise.all并发执行任务
    $all = Promise\all($concurrentTasks)->then(function ($results) use ($start) {
        print_r($results);
        echo "...done in " . (microtime(true) - $start) . PHP_EOL;
    });
    $all->wait();

    sleep(5);
}

function start2(){
    Event\coroutine(function() {

        try {
            $start = microtime(true);

            $result = yield someAsyncOperation();
            $result2 = yield anotherAsyncOperation();
            $result3 = yield lastAsyncOperation();
            echo $result.PHP_EOL;
            echo $result2.PHP_EOL;
            echo $result3.PHP_EOL;

            echo "...done in " . (microtime(true) - $start) . PHP_EOL;
        } catch (\Exception $reason) {

            echo $reason;

        }

    })->wait();
}

function start3(){

//    Loop\setTimeout(function() {
//        someAsyncOperation()->then(function($result){
//            var_dump($result);
//        });
//    },0.1);
//
//    Loop\setTimeout(function() {
//        someAsyncOperation()->then(function($result){
//            var_dump($result);
//        });
//    },0.1);
//
//    Loop\setTimeout(function() {
//        someAsyncOperation()->then(function($result){
//            var_dump($result);
//        });
//    },0.1);

    Loop\nextTick(function() {
        someAsyncOperation()->then(function($result){
            var_dump($result);
        });
    });

    Loop\nextTick(function() {
        someAsyncOperation()->then(function($result){
            var_dump($result);
        });
    });

    Loop\nextTick(function() {
        someAsyncOperation()->then(function($result){
            var_dump($result);
        });
    });

    Loop\run();

    sleep(5);
}
//start();
//start2();
start3();








